<?php
class ml_biz_atNick_nick {
    
    private $rds;
    private $uid;
    
    function __construct(){
        $this->rds = new ml_model_atNick_rdsAtNick();
        $this->uid = null;
    }
        
    
    /**
     * 拼音+英文+其他
     * Enter description here ...
     * @param unknown_type $tag
     * @param unknown_type $tag_id
     */
    private function push_nick_index_byFullLetter($nick){
        $spell = ml_tool_chineseSpell::getFullSpell(trim($nick),'');
        
        if ($spell === false) {
            Tool_logger::dataLog('spell_fail', $nick.'|'.$this->uid, true , false , false);
            return ;
        }
        
        $length = strlen($spell[0]);
        for ($i=0; $i<$length; $i++){
            $key = $spell[0][$i].'@'.$i;
            $re =$this->cal_nick_score($key);
        }
        
        $length = strlen($spell[1]);
        for ($i=0; $i<$length; $i++){
            $key = $spell[1][$i].'@'.$i;
            $re =$this->cal_nick_score($key);
        }
    }
    
    private function remove_nick_index_byFullLetter($nick){
        $spell = ml_tool_chineseSpell::getFullSpell(trim($nick),'');
        
        $length = strlen($spell[0]);
        for ($i=0; $i<$length; $i++){
            
            $key = $spell[0][$i].'@'.$i;
            $re =$this->remove_nick_index($key);
        }
        
        $length = strlen($spell[1]);
        for ($i=0; $i<$length; $i++){
            $key = $spell[1][$i].'@'.$i;
            $re =$this->remove_nick_index($key);
        }
    }
    
    
    /**
     * 汉字+英文+其他
     * Enter description here ...
     * @param unknown_type $tag
     * @param unknown_type $tag_id
     */
    private function push_nick_index_byChinese($nick){
        $spell = trim($nick);
        
        $length = Tool_string::count_all_character($spell);
        for ($i=0; $i<$length; $i++){
            $key = Tool_string::substr_by_charater($spell, $i, 1);            
            $key = $key.'@'.$i;;
            $re = $this->cal_nick_score($key);
        }
    }
    
    private function remove_nick_index_byChinese($nick){
        $spell = trim($nick);
        
        $length = Tool_string::count_all_character($spell);
        for ($i=0; $i<$length; $i++){
            $key = Tool_string::substr_by_charater($spell, $i, 1);            
            $key = $key.'@'.$i;;
            $re = $this->remove_nick_index($key);
        }
    }
    
    
    
    
    /**
     * nick初始化redis
     * Enter description here ...
     * @param unknown_type $tag
     * @param unknown_type $tag_id
     * @param unknown_type $score
     */
    public function init_nick_redis($uid, $nick){
        $nick = trim($nick);
        $this->uid = intval($uid);
        
        //空格不敏感
        //大小写不敏感
        $nick = strtolower($nick);
        
        $re = $this->push_nick_index_byChinese($nick);
        $re = $this->push_nick_index_byFullLetter($nick);
        
        return true;
    }
    
    private function cal_nick_score($mainkey){
    /*    
        $filename = 'cal.txt';

        $somecontent = $mainkey."\n";

    if (!$handle = fopen($filename, 'a')) {
         echo "不能打开文件 $filename";
         exit;
    }

    // 将$somecontent写入到我们打开的文件中。
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "不能写入到文件 $filename";
        exit;
    }

    echo "成功地将 $somecontent 写入到文件$filename";

    fclose($handle);

        //var_dump($b);
        */
        $re =$this->rds->setAtNickNickIndex($mainkey, $this->uid);
    }
    
    private function remove_nick_index($mainkey){
    
        $re =$this->rds->removeAtNickNickIndex($mainkey, $this->uid);
    }
    
    public function remove_nick_redis($uid, $nick){
        $nick = trim($nick);
        $this->uid = intval($uid);
        
        //空格不敏感
        //大小写不敏感
        $nick = strtolower($nick);
        
        $re = $this->remove_nick_index_byChinese($nick);
        $re = $this->remove_nick_index_byFullLetter($nick);
    }
}